package com.sporch.service;

import com.sporch.dao.EndpointRegistrationRepository;
import com.sporch.model.EndpointRegistration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @Author: czy
 * @Date: 2019/6/5 8:57
 */
@Service
public class EndpointRegistrationService {
    @Autowired
    private EndpointRegistrationRepository endpointRegistrationRepository;

    /**
     * 查所有
     * @return
     */
    public List<EndpointRegistration> findAll(){
        List<EndpointRegistration> all = endpointRegistrationRepository.findAll();
        return all;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public EndpointRegistration findById(String id){
        Optional<EndpointRegistration> optional = endpointRegistrationRepository.findById(id);
        if (optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    /**
     * 根据端点id查询
     * @param endpointId
     * @return
     */
    public EndpointRegistration findByEndpointId(String endpointId){
        EndpointRegistration endpoint = endpointRegistrationRepository.findByEndpointId(endpointId);
        return endpoint;
    }

    /**
     * 根据id删除
     * @param id
     */
    public void deleteById(String id){
        endpointRegistrationRepository.deleteById(id);
    }

    /**
     * 添加
     * @param endpointRegistration
     */
    public void add(EndpointRegistration endpointRegistration){
        EndpointRegistration insert = endpointRegistrationRepository.insert(endpointRegistration);

    }

    /**
     * 更新
     * @param endpointRegistration
     */
    public void update(EndpointRegistration endpointRegistration){
        EndpointRegistration byId = findById(endpointRegistration.getId());
        if (byId==null){
            return;
        }
        endpointRegistrationRepository.save(endpointRegistration);
    }

    /**
     * 更新端点状态
     */
    public void updateStatus(String endpointId,String status){
        EndpointRegistration endpoint = findByEndpointId(endpointId);
        endpoint.setStatus(status);
        endpointRegistrationRepository.save(endpoint);
    }

}
